﻿using Gx.Common;
using Gx.Common.Core;
using Gx.Dal.Manage;
using Gx.Model;
using Gx.Model.Entity;
using Gx.Model.ViewModel;
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Web;


namespace Gx.Bll.Manage
{
    public class LKAttachmentBll
    {

        protected LKAttachmentDal dal = new LKAttachmentDal();

        ///// <summary>
        ///// 分页列表
        ///// </summary>
        ///// <param name="input"></param>
        ///// <returns></returns>
        //public PagedList<LKAttachment> GetLKAttachmentPageList(LKAttachmentInput input)
        //{
        //    return dal.GetLKAttachmentPageList(input);
        //}

        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        public List<LKAttachment> GetLKAttachments()
        {
            return dal.GetAll().ToList();
        }

        /// <summary>
        /// 新增附件表
        /// </summary>
        /// <param name="lKAttachment"></param>
        /// <returns></returns>
        public int AddLKAttachment(LKAttachment lKAttachment)
        {
            lKAttachment.CreateTime = DateTime.Now;
            lKAttachment.CreateUserId = LoginHelper.GetUserId();
            lKAttachment.CreateUser = LoginHelper.UserName();
            var result = dal.InsertAnGetId(lKAttachment);
            return Convert.ToInt32(result);
        }

        /// <summary>
        /// 编辑附件表
        /// </summary>
        /// <param name="lKAttachment"></param>
        /// <returns></returns>
        public bool EditLKAttachment(LKAttachment lKAttachment)
        {
            var att = dal.Get(lKAttachment.Id);
            if (att == null)
            {
                return false;
            }
            att.BillNo = lKAttachment.BillNo;
            att.BillType = lKAttachment.BillType;
            return dal.Update(att);
        }

        public int SaveLKAttachment(LKAttachment lKAttachment)
        {
            if (lKAttachment.Id > 0)
            {
                EditLKAttachment(lKAttachment);
            }
            //else
            //{
            //    return AddLKAttachment(lKAttachment);
            //}
            return lKAttachment.Id;
        }

        public bool DeleteLKAttachment(string billNo)
        {
            return dal.DeleteAttachment(billNo);
        }

        /// <summary>
        /// 删除附件表
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteLKAttachment(int id)
        {
            return dal.DeleteById(id);
        }

        public LKAttachment GetLKAttachment(string billNo)
        {
            var att = dal.GetListByWhere($"BillNo='{billNo}'").FirstOrDefault();

            return att;

            //if (att == null) return string.Empty;

            //string url = HttpContext.Current.Request.Url.ToString();
            ////获取当前请求的url中第一个/前的部分
            //url = url.Substring(0, url.IndexOf("/", 8));

            //return $"{url}{GlobalHelper.UploadPath2}{att.AttachmentName}";
        }

        public LKAttachment GetLKAttachmentView(string billNo) 
        {
            string filePath = string.Empty;
            string fileName = string.Empty;
            var attachment = GetLKAttachment(billNo);
            if (attachment != null)
            {
                string url = HttpContext.Current.Request.Url.ToString();
                //获取当前请求的url中第一个/前的部分
                url = url.Substring(0, url.IndexOf("/", 8));
                attachment.AttachmentName = $"{url}{GlobalHelper.UploadPath2}{attachment.AttachmentName}";
                attachment.OriginalName = attachment.OriginalName;
            }
            return attachment;
        }

        //    /// <summary>
        //    /// 获取附件表
        //    /// </summary>
        //    /// <param name="id"></param>
        //    /// <returns></returns>
        //    public LKAttachmentDto GetLKAttachmentById(int id)
        //    {
        //        var bill = dal.Get(id);
        //        //LKIncidentalsDal lKIncidentalsDal = new LKIncidentalsDal();
        //        //var costList = lKIncidentalsDal.GetLKIncidentalsListByContract(id, 1);
        //        return new LKAttachmentDto { LKAttachment = bill };
        //    }
    }
}

